/**
 * @namespace formbuilder
 * @memberOf smatthai
 */
smatthai.formbuilder = (function () {
	/* Configuration setting vars and functions*/
	var table = { };
	
	/**
	 * @memberof smatthai.formbuilder
	 * @method
	 * @protected
	 * @param  {object} args
	 * @return {void}
	 */
	function buildTable(args) {		
		initTable(args);				
		console.log(table.id);			
		
		$('#'+table.id).html(table.render);
	}
	
	function initTable(args) {
		table = table || {};
		table.id = args.divId;
		table.div = $('#'+table.id);
		table.col = {};
		table.col.hide = [];
		loadHeaders(args.headers);
		loadRowData(args.data);
		tblScaffold();
	};
	
	function tblScaffold() {
		var renderTbl = '<table class="table table-striped table-bordered table-hover">';
		renderTbl += '<thead><tr>';
		renderTbl += table.headers.html;
		renderTbl += '</tr></thead>';
		
		renderTbl += '<tbody>';
		renderTbl += table.rows.html;
		renderTbl += '</tbody>';
		
		renderTbl += '</table>';
		table.render = renderTbl;
	};
	
	function loadRowData(data) {
		table.rows = {};
		table.rows.data = data;
		table.rows.render = [];
		
		var rows = "";
		$.each(table.rows.data, function(idx, value) {
			var row = '<tr id="'+ value['id'] + '">';		
			for (var member in value) {
				// check to see if id is in table.col.hide and hide appropriatle
				row += '<td>';
					row += value[member];
				row += '</td>';
			};
			
			row += '</tr>';
			rows += row;
			table.rows.render.push(row);
		});
		
		table.rows.html = rows;
	};
	
	function loadHeaders(headers) {
		table.headers = {};
		table.headers.data = headers;
		table.headers.render = [];
			
		var heads = "";
		$.each(table.headers.data, function(idx, value) {
			var hide = typeof value['hidden'] === undefined || value['hidden'] == "" ? true : false;
			if (hide) {
				table.col.hide.push(value.name);
			}
			
			var head = "";	
			head += '<th>';
				head += value.name;
			head += '</th>';
			
			heads += head;
			table.headers.render.push(head);
		});
		
		table.headers.html = heads;
	};
	
	
	/* End private space */

	// Declare public functions and varibales within the publicInterface object
	//   this object is returned as emp.configManager 
	/**
	 * @memberOf emp.configManager
	 *
	 * @type {Object}
	 */
	var publicInterface = {
		/**
		 * @method
		 * @memberof smatthai.formbuilder
		 * @param isAsync boolean that dictates if the object will be built  asynchronously
		 * @desc Provides a Synchronous method -- blocks thread until object is fully populated.
		 * 		 Asnychronous method -- non-blocking, object is populated when async callbacks
		 * 		 are completed successfully.
		 *       False by default.
		 */
		build: function (args) {
			if (args === 'undefined' || args.isAsync == '' || args.isAsync == null) {
				
			}
			buildTable(args);
		},
		/**
		 * @method
		 * @memberOf emp.configManager
		 * @return {void}
		 */
		loadComplete: function () {
			onloadCallback();
		}

	};
	return publicInterface;
}());